package edu.ynu.e_platform.service;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import edu.ynu.e_platform.common.service.LogicService;
import edu.ynu.e_platform.dao.ItemDao;
import edu.ynu.e_platform.entities.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author manwen Yang
 */
@Slf4j
@Service
public class ItemService extends LogicService<ItemDao, Item, Long> {

    @Resource
    FileStorageService fileStorageService;

    public ItemService(@Autowired ItemDao lr) {
        super(lr);
    }

    public String updateImg(Long itemId, MultipartFile file){
        FileInfo upload = fileStorageService.of(file)
                .upload();
        Integer res = getDAO().updateImgById(upload.getUrl(), itemId);
        if(res==0) {
            fileStorageService.delete(upload.getUrl());
            return "更新失败";
        }
        return "更新成功";
    }
}
